package com.yangjunli.bishe.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangjunli.bishe.core.RequestHolder;
import com.yangjunli.bishe.core.ReturnT;
import com.yangjunli.bishe.entity.Comment;
import com.yangjunli.bishe.entity.CoupleBack;
import com.yangjunli.bishe.mapper.BbsArticleMapper;
import com.yangjunli.bishe.mapper.CommentMapper;
import com.yangjunli.bishe.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器    评论
 * </p>
 *
 * @author jobob
 * @since 2020-04-05
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private ICommentService iCommentService;

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private BbsArticleMapper bbsArticleMapper;

    @PostMapping("/autho")
    public ReturnT saveComment(@RequestBody Comment comment) {
        try {
            String username = RequestHolder.getAccessToken().getUsername();
            String name = RequestHolder.getAccessToken().getName();


            Integer id = RequestHolder.getAccessToken().getId();
            comment.setCreateName(username);
            comment.setCreateId(id);
            comment.setCreateDate(LocalDateTime.now());
            comment.setCreateUserName(name);
            bbsArticleMapper.updateCommentNum(comment.getBbsArticleId());
            iCommentService.save(comment);
            return new ReturnT(200, "保存成功");
        } catch (Exception e) {
            return new ReturnT(100, "评论失败");
        }
    }


    @GetMapping(value = "/queryComment")
    public ReturnT queryComment(Integer pageNo, Integer pageSize) {
        try {

            Long count = commentMapper.queryCount();
            List<Map<String, Object>> item = commentMapper.queryCommnetPage(pageNo, pageSize);
            HashMap<String, Object> result = new HashMap<>();
            result.put("count",count);
            result.put("item",item);
            return new ReturnT(result);
        } catch (Exception e) {
            return new ReturnT(100, "获取失败");
        }
    }


    @GetMapping("/{bbsArticleId}")
    public ReturnT queryComment(@PathVariable("bbsArticleId") Integer bbsArticleId) {
        try {
            List<Comment> commentList = commentMapper.selectByBbsArticleId(bbsArticleId);
            return new ReturnT(200, "保存成功");
        } catch (Exception e) {
            return new ReturnT(100, "评论失败");
        }
    }


    @DeleteMapping("/{id}")
    public ReturnT delComment(Integer id) {
        try {
            commentMapper.deleteById(id);
            return new ReturnT(200, "删除成功");
        } catch (Exception e) {
            return new ReturnT(100, "删除失败");
        }
    }


}
